package com.xiaotu.service.sync;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.xiaotu.entity.*;
import com.xiaotu.service.mysql.MysqlStarService;
import com.xiaotu.service.mysql.MysqlTvService;
import com.xiaotu.service.pgsql.PgsqlStarService;
import com.xiaotu.service.pgsql.PgsqlTvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author zhangxiao
 * @create 2021-09-22 11:21
 */
@Service
public class SyncService {

    @Autowired
    private PgsqlTvService pgsqlTvService;

    @Autowired
    private MysqlTvService mysqlTvService;

    @Autowired
    private MysqlStarService mysqlStarService;

    @Autowired
    private PgsqlStarService pgsqlStarService;

    @Transactional
    public void sync() {
        String specifyDate = DateUtil.formatDate(DateUtil.yesterday());
        //sync tab_tv_info
        syncTabTvInfo(specifyDate);
        //sync tab_star_info
        syncTabStarInfo(specifyDate);
    }

    @Transactional
    public void syncAwars() {
        //同步 艺人获奖
        syncStarAwards();
        //同步 作品获奖
        syncTvAwards();
    }

    //同步 剧播放量相关
    @Transactional
    public void syncTvProcessData() {
        List<TvProcessDataModel> list = pgsqlTvService.getTvProcessData(DateUtil.formatDate(DateUtil.yesterday()));
        if (CollUtil.isNotEmpty(list))
            mysqlTvService.batchInsertTvProcessData(list);
    }

    //同步 剧播放量相关
    @Transactional
    public void syncAllTvProcessData() {
        AtomicInteger count = new AtomicInteger();
        while (true) {
            List<TvProcessDataModel> list = pgsqlTvService.getLimitTvProcessData(count.getAndIncrement(), 5000);
            if (CollUtil.isNotEmpty(list)) {
                mysqlTvService.batchInsertTvProcessData(list);
            } else {
                break;
            }
        }
    }

    public void syncTabTvInfo(String specifyDate) {
        List<TvModel> pgs = pgsqlTvService.needSyncs(specifyDate);
        List<Integer> tvids = pgs.parallelStream().map(p -> p.getTvid()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(tvids)) {
            List<Integer> mysqlIsExist = mysqlTvService.isExist(tvids);
            List<TvModel> updateMysqlList = pgs.stream().filter(f -> (mysqlIsExist.contains(f.getTvid()) == true)).collect(Collectors.toList());
            List<TvModel> insertMysqlList = pgs.stream().filter(f -> (mysqlIsExist.contains(f.getTvid()) == false)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(insertMysqlList))
                mysqlTvService.batchInsert(insertMysqlList);
            if (CollUtil.isNotEmpty(updateMysqlList))
                mysqlTvService.batchUpdate(updateMysqlList);
        }
    }

    public void syncTabStarInfo(String specifyDate) {
        List<StarModel> pgs = pgsqlStarService.needSyncs(specifyDate);
        List<Integer> starids = pgs.parallelStream().map(p -> p.getStarid()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(starids)) {
            List<Integer> mysqlIsExist = mysqlStarService.isExist(starids);
            List<StarModel> updateMysqlList = pgs.stream().filter(f -> (mysqlIsExist.contains(f.getStarid()) == true)).collect(Collectors.toList());
            List<StarModel> insertMysqlList = pgs.stream().filter(f -> (mysqlIsExist.contains(f.getStarid()) == false)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(insertMysqlList))
                mysqlStarService.batchInsert(insertMysqlList);
            if (CollUtil.isNotEmpty(updateMysqlList))
                mysqlStarService.batchUpdate(updateMysqlList);
        }
    }

    private void syncStarAwards() {
        List<ActorAwardModel> actorAwardModels = pgsqlStarService.getStarAwards();
        mysqlStarService.clearStarAwards();
        mysqlStarService.batchInsertStarAwards(actorAwardModels);
    }

    private void syncTvAwards() {
        List<WorksAwardModel> worksAwardModels = pgsqlTvService.getTvAwards();
        mysqlTvService.clearTvAwards();
        mysqlTvService.batchInsertTvAwards(worksAwardModels);
    }

    public void deleteNoUseStar() {
        mysqlStarService.deleteNoUseStar();
    }

    public void deleteNoUseTv() {
        mysqlTvService.deleteNoUseTv();
    }

    public void deleteNoUse() {
        deleteNoUseStar();
        deleteNoUseTv();
    }

}
